<?php

namespace console\controllers;

use Yii;
use yii\console\Controller;
use services\task\TaskService;

/**
 * 任务
 *
 * Class TaskController
 * @package console\controllers
 */
class TaskController extends Controller
{
    /**
     * 定时任务提醒
     *
     * 30 8 * * * php yii task/remind
     */
    public function actionRemind()
    {
        try {
            $startTime = time();

            $service = new TaskService();
            $service->cronTaskRemind();

            $endTime = time();
            $diffTime = $endTime - $startTime;

            echo "success".PHP_EOL;
            echo "开始时间: {$startTime}".PHP_EOL;
            echo "结束时间: {$endTime}".PHP_EOL;
            echo "本次耗时: {$diffTime}".PHP_EOL;
        } catch (\Exception $e) {
            echo 'error: '.$e->getMessage().PHP_EOL;
        }
    }

    /**
     * 定时任务系统日志
     *
     * 30 8 * * * php yii task/update-task
     */
    public function actionUpdateTask()
    {
        try {
            $startTime = time();

            $service = new TaskService();
            $service->cronUpdateTask();

            $endTime = time();
            $diffTime = $endTime - $startTime;

            echo "success".PHP_EOL;
            echo "开始时间: {$startTime}".PHP_EOL;
            echo "结束时间: {$endTime}".PHP_EOL;
            echo "本次耗时: {$diffTime}".PHP_EOL;
        } catch (\Exception $e) {
            echo 'error: '.$e->getMessage().PHP_EOL;
        }
    }
}
